package com.wdhcr.controller.admin;


import com.wdhcr.entity.Photo;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.PhotoService;
import com.wdhcr.utils.ResponseResult;
import com.wdhcr.utils.TencentCOSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.Map;
/*
 * @description: 图片上传的控制类
 * @author: wdhcr
 * @dateTime: 2020/3/31 20:28
 */
@RestController
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    @Autowired
    private TencentCOSUtil tencentCOSUtil;


    /*
     * @description: 管理员获取所有的图片信息
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/31 22:14
     */
    @CrossOrigin
    @GetMapping("getImageList/{page}/{size}")
    public ResponseResult getImageList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"createTime");
        PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Photo> all = photoService.listPhoto(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取图片列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无图片！",all);
    }

    /*
     * @description: 前端普通用户获取发布的图片列表
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/31 22:14
     */
    @CrossOrigin
    @GetMapping("getImagePubList/{page}/{size}")
    public ResponseResult getImagePubList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"createTime");
        PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Photo> all = photoService.findPhoto(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取图片列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无图片！",all);
    }

    @CrossOrigin
    @PostMapping("/photos")
    public ResponseResult post(@RequestParam("file") MultipartFile file) {
        if (file == null || "".equals(file)) {
            return new ResponseResult(ResultCode.FAIL.getCode(),"保存失败！",null);
        } else {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename != null && !originalFilename.isEmpty()) {
                Map<String, String> urlMap = tencentCOSUtil.uploadFileInTencent(file);
                String url = urlMap.get("url");
                Photo photo = new Photo();
                photo.setCreateTime(new Date());
                photo.setPhotoUrl(url);
                photo.setPublished(true);
                Photo photo1 = photoService.savePhoto(photo);
                if (photo1 != null) {
                    return new ResponseResult(ResultCode.SUCCESS.getCode(),"上传成功！",null);
                }
            } else {
                return new ResponseResult(ResultCode.FAIL.getCode(),"请勿重复保存！",null);
            }
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"保存失败！",null);
    }

    @CrossOrigin
    @GetMapping("/photos/{id}/{published}/delete")
    public ResponseResult delete(@PathVariable("id") Long id, @PathVariable("published") Boolean published) {
        Photo photo = photoService.getPhoto(id);
        if (published) {
            photo.setPublished(false);
            photoService.savePhoto(photo);
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"已放置到回收站！",null);
        } else {
            String photoUrl = photo.getPhotoUrl();
            String substring = null;
            if (photoUrl.indexOf("jpg") != -1){
                substring = photoUrl.substring(photoUrl.indexOf("vblog_"), photoUrl.indexOf("jpg") + 3);
            }else {
                substring = photoUrl.substring(photoUrl.indexOf("vblog_"), photoUrl.indexOf("png") + 3);
            }
            boolean s = tencentCOSUtil.deleteFileInTencent(substring);
            if (s) {
                photoService.delete(id);
                return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除成功！",null);
            } else {
                return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除失败！",null);
            }
        }
    }

    @CrossOrigin
    @GetMapping("/photos/{id}/add")
    public ResponseResult add(@PathVariable("id") Long id, RedirectAttributes attributes) {
        Photo photo = photoService.getPhoto(id);
        if (!photo.getPublished()) {
            photo.setPublished(true);
            Photo photo1 = photoService.savePhoto(photo);
            if (photo1 != null) {
                return new ResponseResult(ResultCode.SUCCESS.getCode(),"已重新发布！",null);
            } else {
                attributes.addFlashAttribute("message", "重新发布失败！！");
                return new ResponseResult(ResultCode.SUCCESS.getCode(),"重新发布失败！",null);
            }
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"重新发布失败！",null);
    }

}
